<script type="text/javascript" src="<?=base_url()?>resource/js/layer/layer.js"></script>
<script type="text/javascript" src="<?=base_url()?>resource/js/layer/extend/layer.ext.js"></script>
<script type="text/javascript">
    $(function(){
        trbgmoves('#d3dff2');
        $("select[data-edit-select]").filterSelect();
    });
    /**
     * 获取sap产品三大类
     * @constructor
     */
    function GetSapClassData(type,index){
        var tid='';
        var bid='';
        var load=layer.load(2);
        if(type==1){
            tid=$('#category_id_'+index).val();
        }else{
            tid=$('#category_id_'+index).val();
            bid=$('#brand_id_'+index).val();
        }
        $.post("<?=blang_url('ajax/ajax/GetOMSClassData')?>",{'tid':tid,'bid':bid},function(data){
            if(data) {
                layer.close(load);
                var result = JSON.parse(data);
                var brandData = result['brandData'];
                var modelData = result['modelData'];
                if (brandData) {
                    $('#brand_id_' + index).html("<option value=''>-请选择-</option>");
                    $('#model_id_' + index).html("<option value=''>-请选择-</option>");
                    $('#productSku_' + index).html("<option value=''>-请选择-</option>");
                    for (var i = 0; i < brandData.length; i++) {
                        var brand_Html = "<option value='" + brandData[i]['bid'] + '_' + brandData[i]['name'] + "'>" + brandData[i]['name'] + "</option>";
                        $('#brand_id_' + index).append(brand_Html);
                    }
                }
                if (modelData) {
                    $('#model_id_' + index).html("<option value=''>-请选择-</option>");
                    $('#productSku_' + index).html("<option value=''>-请选择-</option>");
                    for (var j = 0; j < modelData.length; j++) {
                        var model_Html = "<option value='" + modelData[j]['mid'] + '_' + modelData[j]['name'] + "'>" + modelData[j]['name'] + "</option>";
                        $('#model_id_' + index).append(model_Html);
                    }
                }
            }
        });
    }
    //获取SKU
    function GetSapSku(index){
        var tid=$('#category_id_'+index).val();
        var bid=$('#brand_id_'+index).val();
        var mid=$('#model_id_'+index).val();
        var load=layer.load(2);
        $.post("<?=blang_url('ajax/ajax/GetSapSku')?>",{'tid':tid,'bid':bid,'mid':mid},function(data){
            if(data){
                layer.close(load);
                $('#productSku_' + index).html("<option value=''>-请选择-</option>");
                var skuDataArray=JSON.parse(data);
                var skuData=skuDataArray['data'];
                for(var i=0;i<skuData.length;i++){
                    var skuHTML = "<option value='" + skuData[i]['sku']  + "'>" + skuData[i]['products_name'] + "</option>";
                    $('#productSku_' + index).append(skuHTML);
                }
            }
        });
    }
    //修改orderNum
    function orderNum(int){
        var str='';
        if(int==1){
            str= $('#order_num').val().replace('wx','BO');
        }else{
            str= $('#order_num').val().replace('BO','wx');
        }
        $('#order_num').val(str);
    }
    /**
     * 新增产品行
     * @constructor
     */
    function AddTr(){
        /*var ProductHtml= $('#productTr').clone();
        $('#orderTable tr').eq(7).after(ProductHtml);*/
        var length=$('.productTr').length;
        var HTML=productHTML(length+1);
        $('.productTr').eq(length-1).after(HTML);
        console.log(length);
    }
    //删除行
    function DeleteTr(i){
        $('#productTr_'+i).remove();
    }
    /**
     * 生成产品HTML
     * @param i
     * @returns {string}
     */
    function productHTML(i){
        var typeOption='';
        var typeOption=$('#category_id_1').html();
        var HTML='<tr class="productTr" id="productTr_'+i+'">'+
            '<td class="text-left">'+
            '<select class="form-control" name="category_id[]" id="category_id_'+i+'" onchange="GetSapClassData(1,'+i+')" >'+
            typeOption+
        '</select>'+
        '</td>'+
        '<td class="text-left">'+
        '<select class="form-control" name="brand_id[]" id="brand_id_'+i+'" onchange="GetSapClassData(2,'+i+')" >'+
        '<option value="">-请选择-</option>'+
        '</select>'+
        '</td>'+
        '<td class="text-left">'+
        '<select class="form-control" name="model_id[]" id="model_id_'+i+'" onchange="GetSapSku('+i+')" ">'+
        '<option value="">-请选择-</option>'+
        '</select>'+
        '</td>'+
        '<td class="text-left">'+
        '<select class="form-control" name="productSku[]" id="productSku_'+i+'">'+
        '<option value="">-请选择-</option>'+
        '</select>'+
        '</td>'+
        '<td class="text-right">'+
        '<input class="form-control" type="text" id="quantity_'+i+'" name="quantity[]" value="1" size="1">'+
        '</td>'+
        '<td class="text-right">'+
        '<textarea class="form-control" name="customerRemark[]" id="customerRemark_'+i+'" ></textarea>'+
        '</td>'+
        '<td class="text-right"><input class="form-control" type="text" name="imei[]" MaxLenth="15" id="imei_'+i+'"></td>'+
        '<td class="text-right"><button type="button" onclick="DeleteTr('+i+')" data-toggle="tooltip" title="移除" class="btn btn-danger btn-sm"><i class="fa fa-minus-circle"></i></button></td>'+
        '</tr>';
        return HTML;

    }
    /**
     * 提交验证
     * @constructor
     */
    function CheckSub(){
        var length=$('.productTr').length;
        for(var i=1;i<=length;i++){
            var productSku=$('#productSku_'+i).val();
            var category_id=$('#category_id_'+i).val();
            var quantity=$('#quantity_'+i).val();
            var imei=$('#imei_'+i).val();
            if(productSku==''){
                layer.msg(i+'号产品SKU不能为空',{icon:5});
                return false;
            }
            if(isNaN(quantity)){
                layer.msg(i+'号产品数量必须是数字',{icon:5});
                return false;
            }else{
                if(quantity > 999 || quantity <= 0){
                    layer.msg(i+'产品数量必须在0-999之间',{icon:5});
                    return false;
                }
            }
            /*if(quantity==''){
                layer.msg(i+'号产品数量不能为空',{icon:5});
                return false;
            }
            if(imei=='' && category_id !=10){
                layer.msg(i+'号产品IMEI不能为空',{icon:5});
                return false;
            }
            if(imei.length != 15 && category_id !=10){
                layer.msg(i+'号产品IMEI必须是15位',{icon:5});
                return false;
            }*/
        }
        var consumer_name=$('#consumer_name').val();
        var delivery_street_address=$('#delivery_street_address').val();
        var delivery_city=$('#delivery_city').val();
        var delivery_zipcode=$('#delivery_zipcode').val();
        var consumer_phone=$('#consumer_phone').val();
        var consumer_email=$('#consumer_email').val();
        var grandTotal=$('#grandTotal').val();
        var reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        if(consumer_name==''){
            layer.msg('客户姓名不能为空',{icon:5});
            return false;
        }
        if(delivery_street_address==''){
            layer.msg('客户街道不能为空',{icon:5});
            return false;
        }
        if(delivery_city==''){
            layer.msg('客户城市不能为空',{icon:5});
            return false;
        }
        if(delivery_zipcode==''){
            layer.msg('客户邮编不能为空',{icon:5});
            return false;
        }
        if(consumer_phone==''){
            layer.msg('客户电话不能为空',{icon:5});
            return false;
        }
        if(consumer_email==''){
            layer.msg('客户邮箱不能为空',{icon:5});
            return false;
        }
        if(!reg.test(consumer_email)){
            layer.msg('客户邮箱格式错误',{icon:5});
            return false;
        }
        if(isNaN(grandTotal)){
            layer.msg('支付总额必须是数字',{icon:5});
            return false;
        }
    }
</script>
<!--菜单 start-->
<?php $this->load->view('defaults/common/left');?>
<!--菜单 end-->
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
        <h1><?=lang('menu_cjdd')?></h1>
        <?=$this->breadcrumb->generateHTML();?>
        </div>
    </div>
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title"><i class="fa fa-pencil"></i><?=lang('menu_cjdd')?></h3>
            </div>
            <div class="panel-body">
                <div class="table-responsive">
                    <form action="<?=blang_url('orders/LocalOrder/AddLocalOrder')?>">
                         <h3 class="panel-title" style="margin:15px 0 10px 0;">订单信息</h3>
                         <table class="table table-bordered table-hover oms-table">
                             <thead>
                                 <tr>
                                    <td class="text-left">来源单号</td>
                                    <td class="text-left">维修单号</td>
                                    <td class="text-left" style="width: 15%">维修站点</td>
                                    <td class="text-left" style="width: 15%">维修类型</td>
                                    <td class="text-right" style="width: 15%">处理方式</td>
                                    <td class="text-right" style="width: 15%">是否批量单</td>
                                    <td class="text-right" style="width: 15%">所属电商</td>
                                 </tr>
                             </thead>
                             <tbody>
                                 <tr>
                                    <td class="text-left"><input class="form-control" type="text" name="source_oid" readonly id="source_oid" value="<?=$source_oid?>"></td>
                                    <td class="text-left"><input class="form-control" type="text" name="order_num" id="order_num" readonly id="order_num" value="<?=$orderId?>"></td>
                                    <td class="text-left">
                                    <select class="form-control" name="service_center" id="service_center">
                                        <?php foreach($siteList as $row){?>
                                        <option value="<?=$row['id'].'_'.$row['point'].'_'.$row['currency']?>"><?=$row['name_ch']?></option>
                                        <?php }?>
                                    </select>
                                    </td>
                                    <td class="text-left">
                                        <select class="form-control" id="service_type" name="service_type">
                                        <?php foreach($serviceType as $row){?>
                                            <option value="<?=$row['id']?>"><?=$row['name_ch']?></option>
                                        <?php }?>
                                    </select>
                                    </td>
                                    <td class="text-right">
                                        <label class="oms-radio">
                                            <input type="radio" value="1" name="storeOrShip" checked> 发货
                                        </label>
                                        <label class="oms-radio">
                                           <input type="radio" value="0" name="storeOrShip"> 入库
                                        </label>
                                    </td>
                                    <td class="text-right">
                                        <label class="oms-radio">
                                           <input type="radio" value="0" name="batch_operations" onclick="orderNum(0)" checked > 否
                                        </label>
                                        <label class="oms-radio">
                                           <input type="radio" value="1" name="batch_operations" onclick="orderNum(1)"> 是
                                        </label>
                                    </td>
                                    <td class="text-right">
                                        <select class="form-control" id="merchant_num" name="merchant_num" data-edit-select="1">
                                        <option value="">-请选择-</option>
                                        <?php foreach($clientList as $row){?>
                                            <option value="<?=$row['id']?>"><?=$row['name']?></option>
                                        <?php }?>
                                    </select>
                                    </td>
                                 </tr>
                             </tbody>
                         </table>
                         <h3 class="panel-title" style="margin:0 0 10px 0;">产品信息</h3>
                         <table class="table table-bordered table-hover oms-table">
                             <thead>
                                 <tr>
                                    <td class="text-left" style="width:15%">产品类型</td>
                                    <td class="text-left" style="width:15%">品牌</td>
                                    <td class="text-left" style="width:15%">型号</td>
                                    <td class="text-left" style="width:15%">SKU</td>
                                    <td class="text-right">数量</td>
                                    <td class="text-right">Remark</td>
                                    <td class="text-right">IMEI/Serial Number</td>
                                    <td class="text-right"></td>
                                 </tr>
                             </thead>
                             <tbody>
                                 <tr class="productTr" id="productTr">
                                    <td class="text-left">
                                        <select class="form-control" name="category_id[]" id="category_id_1" onchange="GetSapClassData(1,1)" >
                                        <option value="">-请选择-</option>
                                        <?php foreach($productType as $row){?>
                                            <option value="<?=$row['custom_id']?$row['custom_id']:$row['tid']?>"><?=$row['name']?></option>
                                        <?php }?>
                                    </select>
                                    </td>
                                    <td class="text-left">
                                        <select class="form-control" name="brand_id[]" id="brand_id_1" onchange="GetSapClassData(2,1)" >
                                        <option value="">-请选择-</option>
                                    </select>
                                    </td>
                                    <td class="text-left">
                                    <select class="form-control" name="model_id[]" id="model_id_1"  onchange="GetSapSku(1)">
                                        <option value="">-请选择-</option>
                                    </select>
                                    </td>
                                    <td class="text-left">
                                        <select class="form-control" name="productSku[]" id="productSku_1">
                                        <option value="">-请选择-</option>
                                    </select>
                                    </td>
                                    <td class="text-right">
                                        <input class="form-control" type="text" id="quantity_1" name="quantity[]" onkeyup="value=value.replace(/[^\d]/g,'')" value="1" size="1">
                                    </td>
                                    <td class="text-right">
                                        <textarea class="form-control" name="customerRemark[]" id="customerRemark_1" ></textarea>
                                    </td>
                                    <td class="text-right">
                                        <input class="form-control" type="text" name="imei[]" id="imei_1" MaxLenth="15">
                                    </td>
                                    <td class="text-right">
                                        <!-- <input type="button" style="background-color: #00ccff" onclick="AddTr()" value="+"> -->
                                        <button type="button" onclick="AddTr();" data-toggle="tooltip" title="添加" class="btn btn-primary btn-sm"><i class="fa fa-plus-circle"></i></button>
                                    </td>
                                 </tr>
                             </tbody>
                         </table>
                          <h3 class="panel-title" style="margin:0 0 10px 0;">客户信息</h3>
                         <table class="table table-bordered table-hover oms-table">
                             <thead>
                                 <tr>
                                    <td class="text-left">客户姓名<span class="red">*</span></td>
                                    <td class="text-left">街道 <span class="red">*</span></td>
                                    <td class="text-left">城市 <span class="red">*</span></td>
                                    <td class="text-left">州/省</td>
                                    <td class="text-right" style="width: 15%">国家</td>
                                    <td class="text-right">邮编 <span class="red">*</span></td>
                                    <td class="text-right">电话 <span class="red">*</span></td>
                                    <td class="text-right">邮箱 <span class="red">*</span></td>
                                    <td class="text-right">支付总额</td>
                                 </tr>
                             </thead>
                             <tbody>
                                 <tr>
                                    <td class="text-left">
                                        <input class="form-control" type="text" id="consumer_name" name="consumer_name" >
                                    </td>
                                    <td class="text-left">
                                        <input type="text" id="delivery_street_address" name="delivery_street_address" class="form-control">
                                    </td>
                                    <td class="text-left">
                                    <input type="text" id="delivery_city" name="delivery_city" class="form-control">
                                    </td>
                                    <td class="text-left">
                                        <input type="text" id="delivery_state" name="delivery_state" class="form-control">
                                    </td>
                                    <td class="text-right">
                                        <select name="delivery_country" class="form-control">
                                            <option value="ES">ES</option>
                                            <option value="RU">RU</option>
                                            <option value="DE">DE</option>
                                            <option value="GB">GB</option>
                                            <option value="US">US</option>
                                            <option value="PT">PT</option>
                                            <option value="IT">IT</option>
                                            <option value="FR">FR</option>
                                            <option value="BE">BE</option>
                                        </select>
                                    </td>
                                    <td class="text-right">
                                        <input type="text" id="delivery_zipcode" name="delivery_zipcode" class="form-control">
                                    </td>
                                    <td class="text-right">
                                        <input type="text" id="consumer_phone" name="consumer_phone" class="form-control">
                                    </td>
                                    <td class="text-right">
                                        <input type="text" id="consumer_email" name="consumer_email" class="form-control">
                                    </td>
                                    <td class="text-right">
                                        <input type="text" id="grandTotal" name="grandTotal" class="form-control">
                                    </td>
                                 </tr>
                             </tbody>
                         </table>
                         <input class="btn btn-primary pull-right" style="padding:8px 30px;" type="submit" onclick="return CheckSub()" value=" 提 交 ">
                    </form>
                   
                       
                </div>
                    
            </div>
         </div>
    </div>

    <div style="display: none" id="typeList">
        <div style="margin-top: 10px;margin-left: 20px;" id="typeData">
        </div>
    </div>
</div>
   